package com.otaku.zuul.file;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ZuulTokenFilter extends ZuulFilter {


    @Value("${server.port}")
    private String serverPort;

    private final static Logger logger = LoggerFactory.getLogger(ZuulTokenFilter.class);
    /**
     * 执行模式 pre 是转发前执行
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 优先级  越小越先执行
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否启用过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }


    /**
     * 过滤器 业务逻辑书写
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
/*        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String userToken = request.getHeader("userToken");
        if (StringUtils.isEmpty(userToken)){
            currentContext.setSendZuulResponse(false);
            currentContext.getResponse().setContentType("text/html;charset=UTF-8");//中文乱码
            currentContext.setResponseStatusCode(401);
            currentContext.setResponseBody("userToken is null");
        }

        System.out.println("本机端口号"+serverPort);*/
        return null;
    }
}
